window.onload = function (){
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/showAllMerchant?flag=",true)
    xhr.send()
    xhr.onload = function () {
        show.innerHTML='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">商店名</th>\n' +
            '        <th style="border: solid;">用户名</th>\n' +
            '        <th style="border: solid;">法 人</th>\n' +
            '      </tr>'
        for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                "</tr>"
        }
    }
}


let show = document.getElementById("show")
let search = document.getElementById("search")
search.onclick = function () {
    let shopName = document.getElementById("shopName").value;
    let userName = document.getElementById("userName").value
    let legal = document.getElementById("legal").value
    let flag = document.getElementById("flag").value
    const xhr = new XMLHttpRequest();
    let j='';
    if (flag==="所有商家")j='';
    if (flag==="违规商家")j='0';
    if (flag==="正常商家")j='1';
    xhr.open("get", "http://localhost:8080/showAllMerchant?"+"shopName=" + shopName + "&userName=" + userName + "&legal=" + legal + "&flag=" + j, true)
    xhr.send()
    show.innerHTML=''
    xhr.onload = function () {
        show.innerHTML='<tr  style="border: solid;">\n' +
            '        <th style="border: solid;">商店名</th>\n' +
            '        <th style="border: solid;">用户名</th>\n' +
            '        <th style="border: solid;">法 人</th>\n' +
            '      </tr>'
        if (j===''){
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "</tr>"
            }
        }

        if (j==='0'){
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='unBan(this)'>解封</button>" + "</td>" +
                    "</tr>"
            }
        }
        if (j==='1'){
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='ban(this)'>封禁</button>" + "</td>" +
                    "</tr>"
            }
        }


    }
}


function  ban(e) {
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/ban?user_name="+e.id, true)
    xhr.send()
    xhr.onload = function () { alert(e.id+"被封禁")
        const xhr = new XMLHttpRequest();
        xhr.open("get", "http://localhost:8080/showAllMerchant?flag=1",true)
        xhr.send()
        xhr.onload = function () {
            show.innerHTML='<tr  style="border: solid;">\n' +
                '        <th style="border: solid;">商店名</th>\n' +
                '        <th style="border: solid;">用户名</th>\n' +
                '        <th style="border: solid;">法 人</th>\n' +
                '      </tr>'
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='ban(this)'>封禁</button>" + "</td>" +
                    "</tr>"
            }
        }
    }
}


function unBan(e) {
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8080/unBan?user_name="+e.id, true)
    xhr.send()
    xhr.onload = function () {alert(e.id+"解除封禁")
        const xhr = new XMLHttpRequest();
        xhr.open("get", "http://localhost:8080/showAllMerchant?flag=0",true)
        xhr.send()
        xhr.onload = function () {
            show.innerHTML='<tr  style="border: solid;">\n' +
                '        <th style="border: solid;">商店名</th>\n' +
                '        <th style="border: solid;">用户名</th>\n' +
                '        <th style="border: solid;">法 人</th>\n' +
                '      </tr>'
            for (var i = 0; i < JSON.parse(xhr.responseText).length; i++) {
                show.innerHTML += "<tr  style=\"border: solid;\">" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].shopName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].userName + "</td>" +
                    "<td  style=\"border: solid;\">" + JSON.parse(xhr.responseText)[i].legal + "</td>" +
                    "<td  style=\"border: solid;\">" +"<button id='"+JSON.parse(xhr.responseText)[i].userName +"' onclick='unBan(this)'>解封</button>" + "</td>" +
                    "</tr>"
            }
        }
    }
}
